package com.ttai.ui.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import com.ttai.R;
import com.ttai.dagger.componet.activity.DaggerLoginIdComponet;
import com.ttai.dagger.module.activity.LoginIdPresenterModule;
import com.ttai.presenter.activity.AppNamePresenter;
import com.ttai.presenter.activity.HomePresrenter;
import com.ttai.presenter.activity.InsertIdAcountPresenter;
import com.ttai.presenter.activity.LoginIdPresenter;
import com.ttai.ui.base.BaseActivity;
import com.ttai.ui.adapter.LoginIdRecycleViewAdapter;
import com.ttai.untils.Constant;
import com.ttai.untils.ItemClickSupport;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginId extends BaseActivity {
    @Bind(R.id.ib_addidacount)
    Button ibAddidacount;
    @Bind(R.id.rv_idlist)
    RecyclerView rvIdlist;

    @Inject
    LoginIdPresenter loginIdPresenter;

    InsertIdAcountPresenter insertIdAcountPresenter;

    LoginIdRecycleViewAdapter loginIdRecycleViewAdapter;

    private AppNamePresenter appNamePresenter;

    public String appName;
    public String appAcount;
    private String phonenumber;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loginid);
        ButterKnife.bind(this);
        setComponet();

        insertIdAcountPresenter = new InsertIdAcountPresenter(this);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_SECURE, WindowManager.LayoutParams.FLAG_SECURE);

        appNamePresenter = new AppNamePresenter(this);
        appNamePresenter.getAppName();


        loginIdRecycleViewAdapter = new LoginIdRecycleViewAdapter(this);
        rvIdlist.setAdapter(loginIdRecycleViewAdapter);
        rvIdlist.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));

//        loginIdPresenter.getData();
        insertIdAcountPresenter.storeIdAcount(Constant.getToken());

        ItemClickSupport.addTo(rvIdlist).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClicked(RecyclerView recyclerView, final int position, View v) {
                appName =insertIdAcountPresenter.getList().get(position).getId_name();
                appAcount =insertIdAcountPresenter.getList().get(position).getId_acountnumber();
                AlertDialog.Builder builder=new AlertDialog.Builder(LoginId.this);
                builder.setMessage("确定删除?");
                builder.setTitle("提示");

                //添加AlertDialog.Builder对象的setPositiveButton()方法
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(insertIdAcountPresenter.getList().remove(position)!=null){

                            String token = Constant.getToken();
                            if (HomePresrenter.myPhone==null || HomePresrenter.myPhone.isEmpty()){
                                if(Register.phonenumber==null || Register.phonenumber.isEmpty()){
                                    phonenumber = EnterPage.enterphonenumber;
                                }else{
                                    phonenumber =Register.phonenumber;
                                }
                            }else {
                                phonenumber =HomePresrenter.myPhone;
                            }
                            loginIdPresenter.idDelete(token,"1",appAcount);
                        }else {
                        }
//                        Toast.makeText(getBaseContext(), "删除成功", Toast.LENGTH_SHORT).show();
                    }
                });

                //添加AlertDialog.Builder对象的setNegativeButton()方法
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                builder.create().show();
                return true;
            }
        });

        ItemClickSupport.addTo(rvIdlist).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {

            private String acountlogo;

            @Override
            public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                appName = insertIdAcountPresenter.getList().get(position).getId_name();
                acountlogo = insertIdAcountPresenter.getList().get(position).getAcountnumberlogo();
                Intent intent = new Intent();
                intent.putExtra("idname",appName);
                intent.putExtra("acountnumberlogo", acountlogo);
                intent.setClass(LoginId.this,IdItem.class);
                startActivity(intent);
                Toast.makeText(LoginId.this,appName,Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    public void next(View view) {

    }

    @Override
    public void pre(View view) {
        finish();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void setComponet() {
        DaggerLoginIdComponet.builder()
                .loginIdPresenterModule(new LoginIdPresenterModule(this))
                .build()
                .in(this);
    }

    public LoginIdRecycleViewAdapter getLoginIdRecycleViewAdapter() {
        return loginIdRecycleViewAdapter;
    }

    @OnClick(R.id.ib_addidacount)
    public void onViewClicked() {
        Intent intent=new Intent();
        intent.setClass(LoginId.this,AddIdAcount.class);
        startActivity(intent);
    }
}
